/**
 * 请求地址: 
API Key: 
Secret Key: 
 */

import { GaLogManager, GaRequestBodyInputType, GaRequestConfig, GaRequestManager, GaRequestMethod, GaResponseBodyOutputType, GaResponseCode } from "ga_utility_sdk_base";
export interface ChatMessage {
    role: "user" | "assistant" | "system";
    content: string;
  }
  
export class GaChatAiExecutorWx {
    static TAG:string = "GaChatAiExecutorWx";
    static apiKey = "nRcgVkhu1CcgAYSmbz6pnaUS";
    static secretKey = "41hZvprXC5h0xmGa22LGSR7he1qjpahI";
    static url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro";

    static initClass() {

    }

    /**
 * 向ai发送聊天信息,等待其回应.
 * @param message 
 */
    static async chatToAi2(messages:any) {
        try {
            const result = await fetch("https://api.openai.com/v1/chat/completions", {
              method: "post",
              // signal: AbortSignal.timeout(8000),
              // 开启后到达设定时间会中断流式输出
              headers: {
                "Content-Type": "application/json",
                Authorization: `Bearer ${this.apiKey}`,
              },
              body: JSON.stringify({
                model: "gpt-3.5-turbo",
                stream: true,
                messages: messages,
              }),
            });
            return result;
          } catch (error) {
            throw error;
          }
    }

    static async chatToAi(messages:ChatMessage[]) {
        GaLogManager.verbose_c(this.TAG,`chat to ai: ${JSON.stringify(messages)}`);
        let config = new GaRequestConfig(GaRequestMethod.POST
            ,GaRequestBodyInputType.OBJ_2_JSON
            ,GaResponseBodyOutputType.BLOB_2_JSONOBJ);
        config.requestUrl = "http://localhost:5175/apiA/oauth/2.0/token";
        config.requestHeaders = {
            "client_id": "nRcgVkhu1CcgAYSmbz6pnaUS",
            "client_secret": "41hZvprXC5h0xmGa22LGSR7he1qjpahI"
        }
        GaRequestManager.sharedInstance().invokeRequest(config,(status,response,msg,info)=>{
            if(status == GaResponseCode.OK) {
                GaLogManager.verbose_c(this.TAG,'success');
            }
            else {
                GaLogManager.error_c(this.TAG,`status:${status},${msg}`);
            }
        });
    
    }
}